package com.jsb.laundry.jhh.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class Withdrawal {

    public static void main(String[] args) {
//        viewRealName("142724198902101256");
//        System.out.println("asdasdasdas"+viewRealName("142724198902101256"));
//        JSONArray jsonArray = new JSONArray();
//
//        JSONObject map = new JSONObject();
//        map.put("name","娄鹏辉"); //用户名
//        map.put("id_card","411528199805106518"); //身份证
//        map.put("bank_num","6214830324532976");//银行卡号
//        map.put("bank_name","招商银行");//银行名
//        map.put("phone","13027501275"); //手机号
//        map.put("account_amount","10");//体现金额
//        jsonArray.add(map);
//        withdrawal(jsonArray.toJSONString());

//    withdrawal("6228480039149086678","中国农业银行","王宽","18302147671",3.94,"142724198902101256");
//        String wk = realName("142724198902101256", "王宽", "18302147671", "6228480039149086678", "https://jsb-wx.oss-cn-shanghai.aliyuncs.com/shopsInfo/aa55dc274089449fa9cbdf3d81ec667c.jpg", "https://jsb-wx.oss-cn-shanghai.aliyuncs.com/shopsInfo/a29a3e1600844310aec5f5975a931bca.jpg");
//    viewRealName("142724198902101256");
    }

    public static String withdrawal(String yhk,String yh,String xm,String sj,double q,String sfz){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("company_name","聚申宝(上海)企业管理有限公司");
        jsonObject.put("zone_id",40);
        jsonObject.put("batch_no",uuid);
//        JSONObject parse = JSONObject.parseObject(accounts);
//        JSONArray array = parse.getJSONArray("accounts");
        JSONArray accounts = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name",xm);
        jsonObject1.put("id_card",sfz);
        jsonObject1.put("phone",sj);
        jsonObject1.put("bank_name",yh);
        jsonObject1.put("bank_num",yhk);
        jsonObject1.put("account_amount",q);
        jsonObject1.put("account_no",UUID.randomUUID().toString().replaceAll("-", ""));
        accounts.add(jsonObject1);
//        //对于account进行编码
//        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts",accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/batchTransfer/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String withdrawal(String accounts){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("company_name","聚申宝(上海)企业管理有限公司");
        jsonObject.put("zone_id",40);
        jsonObject.put("batch_no",uuid);
        JSONObject parse = JSONObject.parseObject(accounts);
        JSONArray array = new JSONArray();
        array.add(parse);
//        JSONArray accounts = new JSONArray();
//        Map jsonObject1 = new HashMap();
//        jsonObject1.put("name",userName);
//        jsonObject1.put("id_card",idCard);
//        jsonObject1.put("phone",userPhone);
//        jsonObject1.put("bank_name",bankName);
//        jsonObject1.put("bank_num",bankCarNumber);
//        jsonObject1.put("account_amount",money);
//        jsonObject1.put("account_no",UUID.randomUUID().toString().replaceAll("-", ""));
//        accounts.add(jsonObject1);
//        //对于account进行编码
//        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts",array);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"213*123!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/batchTransfer/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 实名
     * @param idCardNo
     * @param name
     * @param phone
     * @param bankNum
     * @param idCardFrontImg
     * @param idCardReverseImg
     * @return
     */
    public static String realName(String idCardNo,String name,String phone,String bankNum,String idCardFrontImg,String idCardReverseImg){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("id_card_no",idCardNo);
        jsonObject.put("name",name);
        jsonObject.put("phone",phone);
        jsonObject.put("bank_num",bankNum);
        jsonObject.put("id_card_front_img",idCardFrontImg);
        jsonObject.put("id_card_reverse_img",idCardReverseImg);
        JSONArray accounts = new JSONArray();

        //对于account进行编码
        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts", accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        JSONObject jsonObject3 = null;
        try {
            jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/real/", jsonObject);
            System.out.println("第三方realName,入参:"+jsonObject+",出参:"+jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject3.toJSONString();
    }

    /**
     * 查看实名
     * @param idCardNo
     * @return
     */
    public static String viewRealName(String idCardNo){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("zone_id",40);
        jsonObject.put("id_card_no",idCardNo);
        JSONArray accounts = new JSONArray();

        //对于account进行编码
        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts", accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/realQuery/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }
}
